package com.swjd.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.swjd.common.utils.Page;
import com.swjd.dao.CustomerDao;
import com.swjd.po.customer;
import com.swjd.po.dict;
import com.swjd.service.CustomerService;

//客户业务逻辑实现
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDao customerdao;
	Page<customer> pages = new Page<>();

	// 查询customer表所有
	@Override
	public List<customer> findAll() {
		List<customer> all = customerdao.findAll();
		return all;
	}

	// 根据条件查询客户对应字典信息
	@Override
	public Page<customer> findCustomerByDict(String cust_name, String cust_source, String cust_industry,
			String cust_level,Integer page,Integer rows) {
		customer customer = new customer();
		// 判断查询条件是否为空 不为空则将条件注入
		if (StringUtils.isNotBlank(cust_name)) {
			customer.setCust_name(cust_name);
		}
		if (StringUtils.isNotBlank(cust_source)) {
			customer.setCust_source(cust_source);
		}
		if (StringUtils.isNotBlank(cust_industry)) {
			customer.setCust_industry(cust_industry);
		}
		if (StringUtils.isNotBlank(cust_level)) {
			customer.setCust_level(cust_level);
		}
		//查询的起始行
		customer.setStart((page-1)*rows);
		//每页数
		customer.setRows(rows);
		// 取出条件，给dao层查询
		List<customer> findCustomerByDict = customerdao.findCustomerByDict(customer);
		// 将查询结果给分页类
		pages.setRows(findCustomerByDict);
		//当前页给分页类
		pages.setPage(page);
		//每页数给分页类
		pages.setSize(rows);
		//查询总记录数
		Integer count = customerdao.findCustomerCount(customer);
		//总条数给分页类
		pages.setTotal(count);
		return pages;
	}

	@Override
	public int addCustomer(customer customer) {
		int i = customerdao.addCustomer(customer);
		return i;
	}

	@Override
	public customer findCustomerByid(Integer cust_id) {
		customer customer = customerdao.findCustomerByid(cust_id);
		return customer;
	}

	@Override
	public int updateCustomerById(customer customer) {
		int i = customerdao.updateCustomerById(customer);
		return i;
	}

	@Override
	public int deleteCustomerById(Integer cust_id) {
	 int i = customerdao.deleteCustomerById(cust_id);
		return i;
	}

}
